package edu.hawaii.duedates.webApp;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
import edu.hawaii.duedates.configuration.DueDatesConfiguration;
import edu.hawaii.duedates.configuration.User;

/**
 * Tests the Index page for the Due Dates 2-0 web application.
 * 
 * @author Aric West
 */
public class TestIndex {
  
  private DueDatesConfiguration config;
  
  /**
   * Sets up a mock DueDatesConfiguration object for use in
   * tests.
   */
  @Before
  public void setUp() {
    this.config = new DueDatesConfiguration();
    User testUser = new User("Test", "tester@hawaii.edu", "password");
    this.config.addUser(testUser);
    this.config.setSmtpServer("mail.hawaii.edu");  
  }
  
  /**
   * Tests that the Index (login page) is rendered correctly and
   * includes all the basic functionalities associated with it.
   */
  @Test
  public void testIndex() {
    // Constant literal strings on advice of PMD.
    final String LOGIN_FORM = "loginForm";     
    final String EMAIL_FIELD = "LoginEmail";
    final String PASSWORD_FIELD = "LoginPassword";
    
    // Start up the WicketTester and check that the start page renders  . 
    WicketTester tester = new WicketTester(new DueDatesApplication(config));
    tester.startPage(Index.class);
    tester.assertRenderedPage(Index.class);
   
    FormTester formTester = tester.newFormTester(LOGIN_FORM);

    //Test starting out with blank fields.
    String emailString = formTester.getTextComponentValue(EMAIL_FIELD);
    assertEquals("Testing text field", "", emailString);
    String passwordString = formTester.getTextComponentValue(PASSWORD_FIELD);
    assertEquals("Testing text field", "", passwordString);
    
    //Test invalid login information.
    formTester.setValue(EMAIL_FIELD, "bademail@gmail.com");
    formTester.setValue(PASSWORD_FIELD, "badpassword");
    formTester.submit("Login");
    tester.assertRenderedPage(Index.class);
    
    //Test valid login. Result Page will render.
    tester.startPage(Index.class);
    FormTester formTester2 = tester.newFormTester(LOGIN_FORM);
    formTester2.setValue(EMAIL_FIELD, "tester@hawaii.edu");
    formTester2.setValue(PASSWORD_FIELD, "password");
    formTester2.submit("Login");
    tester.assertRenderedPage(ResultPage.class);
  }
  
  /**
   * Tests the basic DueDatesApplication constructor.
   */
  @Test
  public void testBasicApplication() {
    WicketTester tester = new WicketTester(new DueDatesApplication());
    tester.startPage(Index.class);
    tester.assertRenderedPage(Index.class);
    
    assertNotNull("Testing wicket:id LoginEmail found.", tester.getTagByWicketId("LoginEmail"));
    assertNotNull("Testing wicket:id LoginPassword found", 
        tester.getTagByWicketId("LoginPassword"));
  }
}
